﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EraNet.WebApp.AppCore;
using EraNet.Services.Messages;
using EraNet.Core.Model.Messages;
using EraNet.Core.Model.Account;
using EraNet.Services.Drivings;
using EraNet.Services.Account;
using EraNet.Core.Model.Enum;

namespace EraNet.WebApp.IMS
{
    public class MessagesController : IMSBasePage
    {
        #region Fields
        private readonly INoticeServices _noticeService;
        private readonly IFeedbackServices _feedbackService;
        private readonly IDriveSchoolService  _driveschoolService;
        private readonly IMemberService _merberService;
        #endregion

        #region Constructors
        public MessagesController(INoticeServices noticeService, IFeedbackServices feedbackService, IDriveSchoolService driveschoolService,IMemberService merberService)
        {
            this._noticeService = noticeService;
            this._driveschoolService = driveschoolService;
            this._feedbackService = feedbackService;
            this._merberService = merberService;
        }
        #endregion


        [HttpGet]
        public ActionResult NoticesDeploy()
        {
            Notices notice = new Notices();
            Employee emp = _driveschoolService.GetLoginClient();
            //查询驾校下面50位学员
            var members = _merberService.GetMemberBySchool(emp.SchoolID, 50,0, true);
            
            ViewBag.Members = members;

            return View(notice);
        }

        public JsonResult seachMember(string memberName)
        {
            Employee emp = _driveschoolService.GetLoginClient();
            var m = _merberService.GetLikeMemberByName(memberName, emp.SchoolID);
            var memberStr = Common.JSONHelper.ToJSONjss(m);
            return Json(memberStr);
        }

        /// <summary>
        /// 反馈意见的详细页面
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public ActionResult FeedBackDetail(int id)
        {
            var entity = _feedbackService.GetNoticeById(id);
            if (entity == null)
            {
                return RedirectToAction("Index","Account");
            }
            return View(entity);
        }


        [HttpPost]
        [ValidateInput(false)]
        public ActionResult NoticesDeploy(string Title, string ShortTitle, string ContentStr, string StartDate, string EndDate, string radMember, string memberids)
        {
            Employee emp = _driveschoolService.GetLoginClient();
            Notices notice = new Notices();
            notice.Title = Title;
            notice.ShortTitle = Title;
            notice.ContentStr = ContentStr;
            notice.StartDate = Convert.ToDateTime(StartDate);
            notice.EndDate = Convert.ToDateTime(EndDate);
            notice.CreaterId = emp.Id;
            notice.NType1 = 1;
            notice.NType2 = (int)NType2.SendToStudent;
            notice.SchoolId = emp.SchoolID;
            notice.SendClient = 0;
            if (radMember.Equals("all"))
            {
                notice.AcceptId = null;
            }
            else
            {
                notice.AcceptId = memberids.TrimEnd(',');
            }

            notice.CreateDate = DateTime.Now;

            _noticeService.Insert(notice);


            return Content("<script>alert('发布成功!');location.href='/IMS/Account/Infos';</script>");

        }
    }
}
